package com.lovo.web.controller;

import com.lovo.web.bean.UserAddressBean;
import com.lovo.web.bean.UserBean;
import com.lovo.web.service.IUserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("userAddress")
public class UserAddressController {
    @Autowired
    IUserAddressService userAddressService;




    /**
     * 添加地址
     * @param userAddress 地址对象
     */
    @RequestMapping("add")
    public String add(UserAddressBean userAddress){
        userAddressService.add(userAddress);
        return "ok";

    }

    /**
     * 删除地址
     * @param addressId 地址id
     */
    @RequestMapping("del/{addressId}")
    public String del(@PathVariable("addressId") long addressId){
        userAddressService.del(addressId);
        return "ok";
    }

    /**
     *根据地址id修改地址
     * @param userAddress  新地址
     */
    @RequestMapping("update")
    public String update(@RequestBody UserAddressBean userAddress){
        userAddressService.update(userAddress);
        return "ok";
    }


    /**
     * 根据地址id修改默认地址
     */
    @RequestMapping("updateAddress/{addressId}")
    public String updateAddress(@PathVariable("addressId") long addressId){
        userAddressService.updateAddress(addressId);
        return "ok";
    }

    /**
     * 根据用户id查询所有地址
     * @param userName 用户名
     * @return 地址集合
     */
    @RequestMapping("findAll/{userName}")
    public List<UserAddressBean> findAll(@PathVariable("userName") String userName){
        List<UserAddressBean> all = userAddressService.findAll(userName);
        return all;
    }
}
